<!doctype html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Tab组件</title>
    <!-- IMPORT CSS -->
    <link rel="stylesheet" href="reset.min.css">
    <style>
        .tab {
            display: flex;
        }

        .tab-item {
            margin-left: 20px;
            padding: 20px;
            color: #bbb;
            font-size: 16px;
            text-align: center;
        }

        .tab-item:hover {
            color: #000;
            cursor: pointer;
        }

        .tab-item.active {
            font-weight: bold;
            border-bottom: 1px solid red;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="tab">
            <div class="tab-item" v-for="item in list" :key="item.tab_id"
                :class="{ active: item.tab_id === currentTabId }" @click="toggleTabItem(item.tab_id)">
                {{item.tab_name}}
            </div>
        </div>
    </div>

    <!-- IMPORT JS -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <script>
        var app = new Vue({
            el: '#app',
            data: {
                list: [],
                currentTabId: '1213',
            },
            created() {
                // 数据处理
                const serverData = Object.freeze([{
                    createdTime: 9912312932,
                    tab_id: '1212',
                    tab_name: '首页',
                }, {
                    createdTime: 9912312933,
                    tab_id: '1213',
                    tab_name: '设置',
                }, {
                    createdTime: 9912312934,
                    tab_id: '1214',
                    tab_name: '购物车',
                }]);
                this.list = serverData;
                this.currentTabId = serverData[0]['tab_id'];
            },
            methods: {
                toggleTabItem(tab_id) {
                    if (this.currentTabId === tab_id) return;
                    this.currentTabId = tab_id;
                }
            }
        });
    </script>
</body>

</html>